var app = getApp();
var orderOffset;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconwidth: 18,
    blankshow: false,
    blanktips: '没有订单记录',
    hasMore: true,
    orders: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var scroll_h = res.windowHeight;
        that.setData({
          scrollheight: scroll_h
        });
      },
    });
    orderOffset = 0;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    this.loadOrderList(orderOffset);
  },
  //  加载订单
  loadOrderList: function (offset) {
    var url = '/index.php/api/Order/return_goods_list';
    var params = {
      'page': offset
    };
    app.request(url, params).then(this.orderListCallBack.bind(this))
  },
  orderListCallBack: function (res) {
    app.hideLoading();
    if(res.status != 'ok'){
      this.setData({
        blankshow: true
      });
      return;
    }
    var tmp_orders = res.result.list;
    if (tmp_orders.length <= 0) {
      if (orderOffset == 0) {
        this.setData({
          blankshow: true,
          hasMore: false
        });
      }
      else {
        this.setData({
          hasMore: false
        });
      }
      return;
    }
    if (tmp_orders.length < 5) {
      this.setData({
        hasMore: false
      });
    }
    var orders = [];
    if (orderOffset > 0) {
      orders = this.data.orders;
    }
    for (var i = 0; i < tmp_orders.length; i++) {
      var goodsinfo = {};
      goodsinfo.id = tmp_orders[i].id;
      goodsinfo.addtime = app.formatDateTime(tmp_orders[i].addtime);
      goodsinfo.orderid = tmp_orders[i].order_id;
      goodsinfo.status = app.globalData.returnStatus[tmp_orders[i].status.toString()];
      goodsinfo.name = tmp_orders[i].goods_name;
      goodsinfo.refund = tmp_orders[i].refund_money;
      goodsinfo.buycount = tmp_orders[i].goods_num;
      goodsinfo.icon = tmp_orders[i].original_img;
      goodsinfo.type = app.globalData.statusDesc[tmp_orders[i].type];
      goodsinfo.typeVal = tmp_orders[i].type;
      if (goodsinfo.icon == '') {
        goodsinfo.icon = '../imgs/goods19.png';
      }
      else {
        goodsinfo.icon = app.globalData.webUrl + goodsinfo.icon;
      }
      var standard = [];
      if (tmp_orders[i].spec_key_name != '' && tmp_orders[i].spec_key_name != null) {
        standard = tmp_orders[i].spec_key_name.split(' ');
        for (var k = 0; k < standard.length; k++) {
          standard[k] = standard[k].split(':')[1];
        }
      }
      goodsinfo.standard = standard;
      orders.push(goodsinfo);
    }
    var blankshow = true;
    if (orders.length > 0) {
      blankshow = false;
    }
    this.setData({
      blankshow: blankshow,
      orders: orders
    });
  },
  //  加载更多订单
  loadMoreOrders: function () {
    if (this.data.hasMore == false) {
      return;
    }
    app.showLoading();
    orderOffset++;
    this.loadOrderList(orderOffset);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  checkExpress: function (event) {
    var expressid = event.currentTarget.dataset.expressid;
    wx.navigateTo({
      url: './express/express?expressid=' + expressid,
    })
  },
  showOrderDetail: function (event) {
    var orderid = event.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: './orderdetail/orderdetail?orderid=' + orderid,
    })
  },
  checkReturnDetail: function (event) {
    var returnid = event.currentTarget.dataset.returnid;
    wx.navigateTo({
      url: './returndetail/returndetail?returnid=' + returnid,
    })
  }
})